package com.sqfw.project.wp.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.domain.TreeSelect;
import com.sqfw.project.wp.domain.WpTreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wp.domain.SysCode;
import com.sqfw.project.wp.service.ISysCodeService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;

/**
 * 工单字典表Controller
 *
 * @author lx
 * @date 2023-06-07
 */
@Api(tags = "树形结构code")
@RestController
@RequestMapping("/wp/sys_code")
public class SysCodeController extends BaseController {
    @Autowired
    private ISysCodeService sysCodeService;

    /**
     * 查询工单字典表列表
     */
    @ApiOperation("查询工单字典表列表")
    @PreAuthorize("@ss.hasPermi('wp:sys_code:list')")
    @GetMapping("/list")
    public AjaxResult list(SysCode sysCode) {
        List<SysCode> list = sysCodeService.selectSysCodeList(sysCode);
        return success(list);
    }

    /**
     * 导出工单字典表列表
     */
    @PreAuthorize("@ss.hasPermi('wp:sys_code:export')")
    @Log(title = "工单字典表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCode sysCode) {
        List<SysCode> list = sysCodeService.selectSysCodeList(sysCode);
        ExcelUtil<SysCode> util = new ExcelUtil<SysCode>(SysCode.class);
        util.exportExcel(response, list, "工单字典表数据");
    }

    /**
     * 获取工单字典表详细信息
     */
    @PreAuthorize("@ss.hasPermi('wp:sys_code:query')")
    @GetMapping(value = "/{autoId}")
    public AjaxResult getInfo(@PathVariable("autoId") Long autoId) {
        return success(sysCodeService.selectSysCodeByAutoId(autoId));
    }

    /**
     * 新增工单字典表
     */
    @PreAuthorize("@ss.hasPermi('wp:sys_code:add')")
    @Log(title = "工单字典表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCode sysCode) {
        return toAjax(sysCodeService.insertSysCode(sysCode));
    }

    /**
     * 修改工单字典表
     */
    @PreAuthorize("@ss.hasPermi('wp:sys_code:edit')")
    @Log(title = "工单字典表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCode sysCode) {
        return toAjax(sysCodeService.updateSysCode(sysCode));
    }

    /**
     * 删除工单字典表
     */
    @PreAuthorize("@ss.hasPermi('wp:sys_code:remove')")
    @Log(title = "工单字典表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{autoIds}")
    public AjaxResult remove(@PathVariable Long[] autoIds) {
        return toAjax(sysCodeService.deleteSysCodeByAutoIds(autoIds));
    }


    /**
     * 查询树形结构syscode
     */
    @PreAuthorize("@ss.hasPermi('getSysCodeTree')")
    @GetMapping("/getSysCodeTree")
    public AjaxResult getSysCodeTree(SysCode sysCode) {

        if (sysCode != null) {
            //查询内容分类
            if (sysCode.getNodetypes().contains("class1")) {
                List<WpTreeSelect> selects = sysCodeService.selectSysCodeTreeList(sysCode);

                return success(selects);
            }
            //查询街道
            if (sysCode.getNodetypes().contains("DISTRICT")) {
                List<WpTreeSelect> selects = sysCodeService.selectSysCodeListByNodetypes(sysCode);

                return success(selects);
            }
        }
        return AjaxResult.error("请正确传参！");

    }

    /**
     * 查询特定的业务分类
     */
    @PreAuthorize("@ss.hasPermi('getSpecificCodeTree')")
    @GetMapping("/getSpecificCodeTree")
    public AjaxResult getSpecificCodeTree() {

        List<WpTreeSelect> selects = sysCodeService.getSpecificCodeTree();
        return success(selects);
    }


}
